<template>
	<view class="page">
		<view class="banner">
			<image class="banner-img" :src="category.wapBannerUrl"></image>
			<view class="banner-title"></view>
		</view>
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in articleList" :key="key"
				@click="goToDetail(value.id,value.goodsType)"
			>
				<view class="uni-media-list">
					<view class="uni-media-list-logo" v-show="value.listPicUrl!=''">
						<image v-if="showImg" :src="value.listPicUrl"></image>
					</view>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top">第{{value.sortOrder}}章&nbsp;&nbsp;{{value.name}}</view>
						<view class="uni-media-list-text-bottom uni-ellipsis">{{value.promotionDesc?value.promotionDesc:''}}</view>
					</view>
					<view class="bg_img" v-show="value.goodsType==3 || value.goodsType==4">
						<image  src="../../static/play.png"></image>
					</view>
				</view>
			</view>
		</view>
		<view v-if="isShowMore" >
			<uni-load-more :status="status" :contentText="contentText"></uni-load-more>
		</view>
		
		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/index/index" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-xiatubiao--copy"></text>
				<text>首页</text>
			</navigator>
			<view class="p-b-btn" @click="share">
				<text class="yticon icon-share"></text>
				<text>分享</text>
			</view>
			<view class="p-b-btn" :class="{active: favorite}" @click="toggleCollect">
				<text class="yticon icon-shoucang"></text>
				<text>收藏</text>
			</view>
			<view class="action-btn-group">
				<button type="primary" class=" action-btn no-border buy-now-btn" @click="lastPage">上一页</button>
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="nextPage">下一页</button>
				
			</view>
		</view>
		
		<!-- #ifdef MP -->
		<view  class="fab-box fab rightTop" style="bottom:120upx;">
			<view class="fab-circle" @click="toggleOrder">
				<text class="iconfont icon-paixu" ></text>
			</view>
		</view>
		<!-- #endif -->
        <music-fab :hasCanPlay="hasCanPlay" ></music-fab>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
    import commonOnLoad from '../../common/common_on_load.js';  
	import {
		mapState,
		mapGetters
	} from 'vuex'
	export default {
		components: {
			uniLoadMore
		},
		computed: {
			...mapState(['forcedLogin']),
			...mapGetters(['hasLogin']),
            ...mapState(['hasCanPlay']),
			...mapState(['userInfo'])  
		},
		data() {
			return {
				order:'desc',
				status: 'more',
				id:null,
				showImg: false,
				articleList: [],
				favorite: false,
				category:{
					bannerUrl:'',
					name:'',
					wapBannerUrl:''
				},
				contentText: {
					contentdown: "下拉显示更多",
					contentrefresh: "加载中",
					contentnomore: "没有更多"
				},
				offset:0,
				limit:8,
				isShowMore:false,
				isHasMore:true,
				isShareReady:false
			}
		},
		onLoad(options) {
			commonOnLoad.commonOnLoad(this.$mRoutesConfig.category,options,options.referenceId);//options.referenceId
			setTimeout(() => {
				this.showImg = true;
			}, 400);

			this.offset = 0;
			if (options && options.id) {
				this.id = options.id;
				this.getCategoryArticles(this.id);
			}
			// #ifdef MP
			if (!this.$store.getters.hasLogin ) {
				this.$store.commit("SET_IS_SILENT_LOGIN", true);
				this.silentLogin();
			}
			// #endif
		},
		onShareAppMessage() {
			let path = "/pages/category/category?id="+ this.id;
			if(this.$store.getters.hasLogin){
				path = path + "&referenceId=" + this.$store.state.userInfo.id;
			}
			return {
				title: this.category.name ? this.category.name : this.$mConfig.descTxt,
				path: path,
				imageUrl:this.category.wapBannerUrl ? this.category.wapBannerUrl : this.$mConfig.logoImgUrl
			}
		},
		// #ifndef MP
		onNavigationBarButtonTap(e) {
			const index = e.index;
			if (index === 0) {
				this.toggleOrder();
			}
		},
		// #endif
		onPullDownRefresh() {
			this.articleList = [];
			this.offset = 0;
			this.isHasMore = true;
			this.getCategoryArticles(this.id);
		},
		onReachBottom() {
			this.getCategoryArticles(this.id);
		},
		methods: {
			lastPage(){
				this.articleList = [];
				this.offset = this.offset - (2* this.limit);
				if(this.offset < 0){
					this.offset = 0;
				}
				this.isHasMore =true;
				this.getCategoryArticles(this.id);
			},
			nextPage(){
				this.articleList = [];
				this.getCategoryArticles(this.id);
			},
			share(){
				let path = "/pages/category/category?id="+ this.id;
				// #ifdef H5  
				this.showShareClass = 'show';
				// #endif  
				// #ifdef MP-WEIXIN
				let shareOPtions = {
					provider: "weixin",
					scene: 'WXSceneSession',   
					type: 5,
					success: (e) => {
						console.log('success', e);
					},
					fail: (e) => {
						console.log('fail', e)
					},
					complete:function(){
						console.log('分享操作结束!')
					}
				};
				shareOPtions.imageUrl = this.category.wapBannerUrl ? this.category.wapBannerUrl : this.$mConfig.logoImgUrl;
				shareOPtions.title = this.category.name;
				
				if(this.$store.getters.hasLogin){
					path = path + "&referenceId=" + this.$store.state.userInfo.id;
				}
				
				shareOPtions.miniProgram = {
					id:this.$mConfig.xcxId,
					path: path,
					webUrl:this.$mConfig.baseUrl + "/api/shorturl/" + this.$mConfig.mpAppId + "/category?id=" + this.id,
					type: 0
				};
				console.log(JSON.stringify(shareOPtions));
				uni.share(shareOPtions);
				// #endif  
			},
			//收藏
			async toggleCollect(){
				var query = {};
				if(this.favorite){  //取消
					let data = await this.$apis.delCategoryCollect(this.id,query);
					this.category.collect = 0;
				}else{
					let data = await this.$apis.addCategoryCollect(this.id,query);
					this.category.collect = 1;
				}
				this.favorite = !this.favorite;
			},
			async getCategoryArticles(id){
				if(!this.isHasMore){
					return;
				}
				this.status = 'loading';
				this.isShowMore = true;
				var query = {
					offset:this.offset,
					limit:this.limit,
					order:this.order,
				};
				let data = await this.$apis.getArticleList(id,query);
				this.offset = this.offset + this.limit;
				data.articles.forEach(item => {
					this.articleList.push(item);
				});
				this.category = data.category;
				uni.stopPullDownRefresh();
				uni.setNavigationBarTitle({
					title: this.category.name,
				});
				if(data.articles.length == 0){
					this.status = 'noMore';
					this.isHasMore = false;
				}else{
					this.isShowMore = false;
					this.isHasMore = true;
				}
				if(!this.isShareReady){
					this.isShareReady = true;
					this.shareReady();
				}
				if(this.category.collect > 0){
					this.favorite = true;
				}
			},
			shareReady(){
				// #ifdef H5  
				if (this.$wechat && this.$wechat.isWechat()) {  
					var url = this.$mConfig.baseUrl + "/api/shorturl/" + this.$mConfig.mpAppId + "/category?id=" + this.id;
					if(this.$store.getters.hasLogin){
						url = url + "&referenceId=" + this.$store.state.userInfo.id;
					}
					 this.$wechat.share({  
						  desc: this.category.frontDesc,  
						  img: this.category.wapBannerUrl,  
						  title:this.category.name,  
					},url);  
				}  
				// #endif  
			},
			toggleOrder(){
				if(this.order == 'desc'){
					this.order = 'asc';
				}else{
					this.order = 'desc';
				}
				this.articleList = [];
				this.offset = 0;
				this.isHasMore = true;
				this.getCategoryArticles(this.id);
			}
		}
	}
</script>

<style scoped lang="scss">
	.f-header{
		display:flex;
		align-items:center;
		height: 140upx;
		padding: 6upx 30upx 8upx;
		background: #fff;
	}	
	.f-header image{
		flex-shrink: 0;
		width: 80upx;
		height: 80upx;
		margin-right: 20upx;
	}
	.f-header .tit-box{
		flex: 1;
		display: flex;
		flex-direction: column;
	}
	.f-header .tit{
		font-size: $font-lg +2upx;
		color: #font-color-dark;
		line-height: 1.3;
	}
	.f-header .tit2{
		font-size: $font-sm;
		color: $font-color-light;
	}
	.f-header .icon-you{
		font-size: $font-lg +2upx;
		color: $font-color-light;
	}
	.uni-media-list-logo image{
		border-radius: 10%;
	}
	.uni-media-list{
		.bg_img{
			height: 80upx;
			width: 80upx;
			padding-top:15upx;
			image {
				height: 80upx;
				width: 80upx;
			}
		}
	}
	
	.banner {
		height: 360upx;
		overflow: hidden;
		position: relative;
		background-color: #ccc;
	}
	.banner-img {
		width: 100%;
		height:100%;
	}
	.banner-title {
		max-height: 84upx;
		overflow: hidden;
		position: absolute;
		left: 30upx;
		bottom: 30upx;
		width: 90%;
		font-size: 32upx;
		font-weight: 400;
		line-height: 42upx;
		color: white;
		z-index: 11;
		color: #3C3E49;
	}
	
	
	// #ifdef MP  
	.fab-box {
		position: fixed;
		display: flex;
		justify-content: center;
		align-items: center;
		z-index: 2;
	}

	.fab-box.top {
		width: 60upx;
		height: 60upx;
		right: 30upx;
		bottom: 60upx;
		border: 1px #5989b9 solid;
		background: #6699cc;
		border-radius: 10upx;
		color: #fff;
		transition: all 0.3;
		opacity: 0;
	}

	.fab-box.fab {
		z-index: 30;
	}

	.fab-box.fab.rightTop {
		right: 60upx;
		top: -320upx;
	}

	.fab-circle {
		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		width: 80upx;
		height: 80upx;
		background: #007AFF;
		/* background: #5989b9; */
		border-radius: 50%;
		box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.2);
		z-index: 11;
	}

	.fab-circle .uni-icon-plusempty {
		color: #ffffff;
		font-size: 58upx;
		transition: all 0.3s;
		font-weight: bold;
	}

	.fab-circle .uni-icon-plusempty.active {
		transform: rotate(135deg);
	}	
	//#endif  
	/* 底部操作菜单 */
	.page-bottom{
		position:fixed;
		right: 30upx;
		bottom:30upx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690upx;
		height: 100upx;
		background: rgba(255,255,255,.9);
		box-shadow: 0 0 20upx 0 rgba(0,0,0,.5);
		border-radius: 16upx;
		
		.p-b-btn{
			display:flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-sm;
			color: $font-color-base;
			width: 96upx;
			height: 80upx;
			.yticon{
				font-size: 40upx;
				line-height: 48upx;
				color: $font-color-light;
			}
			&.active, &.active .yticon{
				color: $uni-color-primary;
			}
			.icon-fenxiang2{
				font-size: 42upx;
				transform: translateY(-2upx);
			}
			.icon-shoucang{
				font-size: 46upx;
			}
		}
		.action-btn-group{
			display: flex;
			height: 76upx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20upx 40upx -16upx #fa436a;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30,#fa436a,#F56C6C);
			margin-left: 20upx;
			position:relative;
			&:after{
				content: '';
				position:absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28upx;
				width: 0;
				border-right: 1px solid rgba(255,255,255,.5);
			}
			.action-btn{
				display:flex;
				align-items: center;
				justify-content: center;
				width: 180upx;
				height: 100%;
				font-size: $font-base ;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
</style>

